package cn.ztuo.bitrade.controller.system;


import cn.ztuo.bitrade.controller.common.BaseAdminController;
import cn.ztuo.bitrade.entity.SysCf;
import cn.ztuo.bitrade.service.SysCfService;
import cn.ztuo.bitrade.util.MessageResult;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/system/cf")
public class SyscfController extends BaseAdminController {

    @Autowired
    private SysCfService sysCfService;

    /**
     * 获取参数
     * @return
     */
    @RequiresPermissions("system:cf:findAll")
    @PostMapping("/findAll")
    public MessageResult findAll(){
        return MessageResult.getSuccessInstance("成功",sysCfService.configAll());
    }
    /**
     * 批量修改参数
     * @return
     */
    @RequiresPermissions("system:cf:update")
    @PostMapping("/update")
    @Transactional
    public MessageResult updateConfig(@RequestParam(value = "data") String data){
        JSONArray jsonArray = JSONArray.fromObject(data);
        for(int i=0;i<jsonArray.size();i++){
            JSONObject job = jsonArray.getJSONObject(i);
            SysCf info = sysCfService.configOneCname((String) job.get("cname"));
            info.setValue((String) job.get("value"));
            sysCfService.save(info);
        }
        return MessageResult.success("修改成功");
    }

}
